package com.gxd.web.servlet;

import com.alibaba.fastjson.JSON;
import com.gxd.domain.User;
import com.gxd.service.AdminService;
import com.gxd.service.impl.AdminServiceImpl;
import com.gxd.util.NotNull;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/adminLogin")
public class AdminServlet extends ServletBase{
    AdminService adminService = new AdminServiceImpl();
    public String deleteUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        int id = Integer.parseInt(request.getParameter("id"));

        adminService.deleteUser(id);
        return "redirect:/adminLogin?method=getUserList";
    }



        public void selectMore(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = request.getParameter("username");
        String gender = request.getParameter("gender");
        StringBuilder where = new StringBuilder(" where 1=1 ");
        List<Object> params = new ArrayList<>();
        if(!NotNull.isEmpty(username)){
            where.append(" and username like ? ");
            params.add("%"+username+"%");
        }

        if(!NotNull.isEmpty(gender)){
            where.append(" and gender like ? ");
            params.add("%"+gender+"%");
        }

        List<User> list =adminService.findByMore(where.toString(),params);
        String j = JSON.toJSONString(list);
        response.setCharacterEncoding("utf-8");
        response.getWriter().print(j);
    }
        public void invalid(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<User> userList=adminService.findByinvalid();
        String j = JSON.toJSONString(userList);
        response.setCharacterEncoding("utf-8");
        response.getWriter().print(j);

    }
        public String loginLOut(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.getSession().removeAttribute("admin");
        return "redirect:/admin/login.jsp";

    }


        public void getUserList(HttpServletRequest request, HttpServletResponse response) throws Exception {

        List<User> userList=adminService.findByUid();
        String j = JSON.toJSONString(userList);
        response.setCharacterEncoding("utf-8");
        response.getWriter().print(j);


    }
    public String login(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if(NotNull.isEmpty(username)||NotNull.isEmpty(password)){
            return "redirect:/admin/login.jsp";
        }

        User admin=adminService.findByUAndP(username,password);
        if(admin!=null){
            request.getSession().setAttribute("admin",admin);
            return "redirect:/admin/admin.jsp";
        }else{
            return "redirect:/admin/login.jsp";
        }

    }



}
